package org.jeecg.modules.demo3.fly.utils;


import org.jeecg.modules.demo3.fly.entity.tcp.Clents;
import org.jeecg.modules.demo3.fly.entity.tcp.DistributionDataBean;
import org.jeecg.modules.demo3.fly.entity.tcp.Radio_Rec_Enum;

import javax.swing.plaf.synth.SynthOptionPaneUI;

/**
 * @author wjn
 * @version V1.0
 * @date 2021/5/25 15:23
 * @Copyright © 2021/5/25 深圳卓翼航空无人机有限公司
 */
//数据解析类
public class DateShift {
    private long flight_Control_Time_Radio_Rec = 0;
    private long flight_Control_Time_Radio_Rec_Pre = 0;
    public byte[] Radio_Data = new byte[400];//飞控一般数组   解析后的数据数组
    private byte[] Radio_configureParm = new byte[400];//配置参数的数组
    private byte[] Radio_Temp_Data = new byte[400];//400临时存储空间
    private byte Len_Cnt = 0;
    private short Radio_Massage_Len = 0;//数据长度
    private short Read_Len_Pre = 0;//
    private byte rData, Frame_Type; // rData 当前读取位置的数据    Frame_Type 表示数据帧类型：0：飞行状态数据，1：配置参数数据
    Radio_Rec_Enum radio_rec_Step = Radio_Rec_Enum.HANDER1_RD; //飞控结束字  飞行数据最后一个数据

    ParsingData parsingData = new ParsingData(); //调用数据解析类  返回数据类

    public DistributionDataBean issue_Agreement(byte[] bytesdata, int D_Len) {  //number 标识客户端
        DistributionDataBean distributionDataBean = null;
        byte Avalib_Data_Sum = 0; //解析的有效数据和运算结果
        short Raed_lEN, i;
        i = 0;//当前读取位置的索引
        do {
            if (D_Len == 0) return null;
                switch (radio_rec_Step) {//飞行结束字
                    case HANDER1_RD:   //同步字 1
                        while (i < D_Len && radio_rec_Step == Radio_Rec_Enum.HANDER1_RD) {
                            rData = bytesdata[i];
                            if (rData == (byte) 0xEB || rData == (byte) 0xAA) {
                                radio_rec_Step = Radio_Rec_Enum.HANDER2_RD;
                            }
                            i++;
                        }
                        break;

                    case HANDER2_RD:   //同步字 二
                        if (i < D_Len) {
                            rData = bytesdata[i];
                            switch (rData) {
                                case (byte) 0x90: {
                                    radio_rec_Step = Radio_Rec_Enum.CLASS_RD;
                                    Frame_Type = 0;
                                }
                                break;
                                case (byte) 0x63: {
                                    radio_rec_Step = Radio_Rec_Enum.CLASS_RD;
                                    Frame_Type = 1;
                                }
                                break;
                                default: {
                                    radio_rec_Step = Radio_Rec_Enum.HANDER1_RD;
                                }
                                break;
                            }
                            i++;
                        }
                        break;

                    case CLASS_RD:   //方式字
                        if (i < D_Len) {
                            rData = bytesdata[i];
                            i++;
                            Len_Cnt = 0;
                            radio_rec_Step = Radio_Rec_Enum.LEN_RD;
                        }
                        break;

                    case LEN_RD:   //数据长度
                        byte[] leng_rd = new byte[2];
                        while (i < D_Len && radio_rec_Step == Radio_Rec_Enum.LEN_RD) {
                            rData = bytesdata[i];
                            leng_rd[Len_Cnt] = rData;
                            Len_Cnt++;
                            if (Len_Cnt >= 2) {
                                Radio_Massage_Len = Base_Conversion.byte2shortLittleEndian(leng_rd);
                                if (Radio_Massage_Len < 0) {
                                    Radio_Massage_Len = (short) (32768 + Radio_Massage_Len);
                                }
                                radio_rec_Step = Radio_Rec_Enum.GET_RD;
                                if (Radio_Massage_Len > 1024) {  //数据长度超过预设buff长度
                                    radio_rec_Step = Radio_Rec_Enum.HANDER1_RD;
                                }
                                Read_Len_Pre = 0;
                                Len_Cnt = 0;
                            }
                            i++;
                        }
                        break;

                    case GET_RD:   //数据内容
                        if (i < D_Len) {
                            if ((D_Len - i) <= (Radio_Massage_Len - Read_Len_Pre)) {   //D_len-i，buff里剩余数据长度，Massage_Len - Read_Len_Pre该帧数据还需要读取的数据长度
                                Raed_lEN = (short) (D_Len - i);
                            } else {
                                Raed_lEN = (short) (Radio_Massage_Len - Read_Len_Pre);
                            }
                            if (Raed_lEN >= 400) {  //Read_Len_Pre + Raed_lEN >= 400
                                radio_rec_Step = Radio_Rec_Enum.HANDER1_RD;
                            } else {
                                Base_Conversion.mymemcpy(Radio_Temp_Data, Read_Len_Pre, bytesdata, i, Raed_lEN); // i=5  Raed_lEN = 96  Read_Len_Pre=0
                                Read_Len_Pre = (short) (Read_Len_Pre + Raed_lEN);
                                if (Read_Len_Pre >= Radio_Massage_Len) { // Radio_Massage_Len = 96
                                    if (Frame_Type == 0) {
                                        radio_rec_Step = Radio_Rec_Enum.SUM_RD;//SUM_RD;
                                        Avalib_Data_Sum = 0;
                                    } else if (Frame_Type == 1) {
                                        radio_rec_Step = Radio_Rec_Enum.TAIL1_RD;//TAIL1_RD;
                                    }
                                }
                                i += Raed_lEN;
                            }
                            //  Read_Len_Pre = 0;
                        }
                        break;

                    case SUM_RD:   //校验和
                        if (i < D_Len) {   //  D_Len原始数据 长度  104    i = 101
                            rData = bytesdata[i];
                            for (int m = 0; m < Radio_Massage_Len; m++) {
                                Avalib_Data_Sum += Radio_Temp_Data[m];
                            }
                            if (rData == Avalib_Data_Sum) {
                                radio_rec_Step = Radio_Rec_Enum.TAIL1_RD;
                            } else {
                                radio_rec_Step = Radio_Rec_Enum.HANDER1_RD;
                            }
                            i++;
                        }
                        break;

                    case TAIL1_RD:   //结束字 一
                        if (i < D_Len) {
                            rData = bytesdata[i];
                            if (rData == (byte) 0x09) {
                                radio_rec_Step = Radio_Rec_Enum.TAIL2_RD;
                            } else {
                                radio_rec_Step = Radio_Rec_Enum.HANDER1_RD;
                            }
                            i++;
                        }
                        break;

                    case TAIL2_RD:   //结束字 二
                        if (i < D_Len) {
                            rData = bytesdata[i];
                            if (rData == (byte) 0xD7) {
                                if (Frame_Type == 0) {
                                    Base_Conversion.mymemcpy(Radio_Data, (short) 0, Radio_Temp_Data, (short) 0, (int) Radio_Massage_Len);//飞控协议
                                    //调用数据解析类  返回数据类
                                    distributionDataBean = parsingData.dataDistribution(Radio_Data);
                                    //int number = distributionDataBean.getSerial_number();
                                    //clents.setNumber(number);
                                    //fileInsert.WriterReader(distributionDataBean,number,clents); // 调用输入流 将数据对象保存在本地
                                } else if (Frame_Type == 1) {
                                    Base_Conversion.mymemcpy(Radio_configureParm, (short) 0, Radio_Temp_Data, (short) 0, (int) Radio_Massage_Len);//配置参数协议
                                }

                            }
                            radio_rec_Step = Radio_Rec_Enum.HANDER1_RD;
                            i++;
                        }
                        break;
                }
            } while (i < D_Len) ;
        return distributionDataBean;
    }
}
